package org.hwua.production.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
/**
 * 查看在线人数
 * @author hwua
 *
 */
@Controller
public class SessionController {
	@Resource
	private SessionDAO sessionDAO;

	@RequestMapping("/sessionsList")
	public String list(Model model) {
		Collection<Session> sessions = sessionDAO.getActiveSessions();
		List<Session> list=new ArrayList<Session>();
		for(Session s:sessions){
			if(s.getAttribute("kickout") == null){
				list.add(s);
			}
		}
		model.addAttribute("sessions", list);
		model.addAttribute("sesessionCount", list.size());
		return "sessions_list";
	}
	
	@RequestMapping("/{sessionId}/forceLogout")  
    public String forceLogout(@PathVariable("sessionId") String sessionId,   
        RedirectAttributes redirectAttributes) {  
        try {  
            Session session = sessionDAO.readSession(sessionId);  
            if(session != null) {  
                session.setAttribute("kickout", true);  
                session.setTimeout(0);
            }  
        } catch (Exception e) {
        	e.printStackTrace();
        }  
        redirectAttributes.addFlashAttribute("msg", "强制退出成功！");  
        return "redirect:/sessionsList";  
    }  
}
